/*
*  Copyright (c) 2023-2025 苏州棱镜七彩信息科技有限公司 (Suzhou PrismAurora Information Technology Co.,Ltd)
*  sbom-generate is licensed under Mulan PSL v2.
*  You can use this software according to the terms and conditions of the Mulan PSL v2.
*  You may obtain a copy of Mulan PSL v2 at:
*           http://license.coscl.org.cn/MulanPSL2
*  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *  See the Mulan PSL v2 for more details.
* */
package com.ljqc.abs.cli;

import com.ljqc.abs.Parser;
import com.ljqc.abs.sbom.SBom;
import com.ljqc.entity.CliCheckFileInfo;
import com.ljqc.entity.DepInfo;
import com.ljqc.entity.PackageInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CliParser
 * @Description 命令行解析超类
 * @Date 2025/3/12 14:28
 * @Version V1.0
 **/
public abstract class CliParser implements Parser<PackageInfo> {

    /*需要检测的文件名称列表*/
    protected final List<CliCheckFileInfo> needCheckFileNames = new ArrayList<>();

    /*需要解析的文件路径列表*/
    protected final List<File> needParserFilePaths = new ArrayList<>();

    /*分隔符*/
    public static final String DELIMITER = "#ljqc#";


    public CliParser() {
    }

    /**
     * @param file
     * @param sbomFormat
     * @date 2025/3/19 15:52
     */
    public void filterAdd(File file, SBom sbomFormat) {
        String fileName = file.getName();
        if (!SBom.SWID_XML.equals(sbomFormat) && needCheckFileNames.stream()
                .anyMatch(nameInfo ->
                        nameInfo.isEndWith()
                                ? fileName.endsWith(nameInfo.getName())
                                : fileName.equals(nameInfo.getName()))) {
            needParserFilePaths.add(file);
        }
    }

    public boolean isNeedParser() {
        return !needParserFilePaths.isEmpty();
    }

    /**
     * 解析入口
     *
     * @param
     * @return java.util.List<com.ljqc.entity.PackageInfo>
     * @date 2025/3/13 16:52
     */
    public List<PackageInfo> parser() {
        return parser(needParserFilePaths);
    }

    protected DepInfo convert(String sourceData) {
        String[] split = sourceData.split(DELIMITER);
        int level = Integer.parseInt(split[0]);
        String[] depData = split[1].split(":");
        return DepInfo.builder().level(level).depData(depData).build();
    }
}
